<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="40px">
      <el-form-item label="时间">
        <el-date-picker
            ref="queryRef"
            v-model="queryParams.startTime"
            type="datetime"
            placeholder="开始时间"
            style="margin-right: 10px;"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
        />
        <el-date-picker
            ref="queryRef"
            v-model="queryParams.endTime"
            type="datetime"
            placeholder="结束时间"
            style="margin-right: 10px;"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
        />
        <el-button @click="handleQuery">加载数据</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['device:errLog:export']"
        >导出数据</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table scrollbar-always-on v-loading="loading" :data="errLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="设备序列号" align="center" prop="deviceSn" />
      <el-table-column label="时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="事件类型" align="center" prop="eventType" />
      <el-table-column label="事件内容" align="center" prop="eventContent" />


    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改异常日志对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="errLogRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备序列号" prop="deviceSn">
          <el-input v-model="form.deviceSn" placeholder="请输入设备序列号" />
        </el-form-item>
        <el-form-item label="事件内容" prop="eventContent">
          <el-input v-model="form.eventContent" placeholder="请输入事件内容" />
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ErrLog">
import { listErrLog, getErrLog, delErrLog, addErrLog, updateErrLog } from "../../../api/slope/data/errLog.js";

const { proxy } = getCurrentInstance();

const errLogList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const props = defineProps({
  device: {
    type: Object,
    default: () => ({})
  }
});
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deviceSn:props.device.deviceSn,
    startTime:null,
    endTime:null,
  },
  rules: {
    deviceSn: [
      { required: true, message: "设备序列号不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询异常日志列表 */
function getList() {
  loading.value = true;
  listErrLog(queryParams.value).then(response => {
    errLogList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });

}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    deviceSn: null,
    updateTime: null,
    eventType: null,
    eventContent: null,
    deptId: null
  };
  proxy.resetForm("errLogRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加异常日志";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getErrLog(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改异常日志";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["errLogRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateErrLog(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addErrLog(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除异常日志编号为"' + _ids + '"的数据项？').then(function() {
    return delErrLog(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('device/errLog/export', {
    ...queryParams.value
  }, `errLog_${new Date().getTime()}.xlsx`)
}

getList();
</script>
